
package com.zhonghong.ui;

import com.zhonghong.entrance.Xls2Xml;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class LayoutUI implements ActionListener {
    private JFrame jf;
    private JPanel contentPane;
    private JButton excelBtn;
    private JButton xmlBtn;
    private JButton okBtn;
    private JButton cancelBtn;

    private JFileChooser fileChooser;
    private File xmlFile;
    private File xlsFile;
    private File dest;

    private static final int WIDTH = 300;
    private static final int HEIGHT = 100;
    private static final String TITLE = "strings.xml生成工具";
    private static final String XML_BTN_STR = "选择XML文件";
    private static final String XLS_BTN_STR = "选择Excel文件";
    private static final String ENTER_OK = "确定选择";
    private static final String ENTER_CANCEL = "重新选择";

    /** 用户工作主目录 */
    private static String PATH = System.getProperty("user.dir");

    public LayoutUI() {
        jf = new JFrame(TITLE);
        jf.setSize(WIDTH, HEIGHT);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        jf.setLocation(screenSize.width / 4, screenSize.height / 4);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
        jf.setResizable(false);

        contentPane = new JPanel();
        jf.setContentPane(contentPane);

        excelBtn = new JButton(XLS_BTN_STR);
        xmlBtn = new JButton(XML_BTN_STR);
        okBtn = new JButton(ENTER_OK);
        cancelBtn = new JButton(ENTER_CANCEL);
        okBtn.setEnabled(false);

        contentPane.setLayout(new FlowLayout());
        contentPane.add(excelBtn);
        contentPane.add(xmlBtn);
        contentPane.add(okBtn);
        contentPane.add(cancelBtn);

        dest = new File(PATH);

        initListener();
    }

    private void initListener() {
        excelBtn.addActionListener(this);
        xmlBtn.addActionListener(this);
        okBtn.addActionListener(this);
        cancelBtn.addActionListener(this);
    }

    /**
     * 按钮事件处理
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        String eventFrom = e.getActionCommand();
        switch (eventFrom) {
            case XML_BTN_STR: {
                fileChooser = new JFileChooser(PATH);
                fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                fileChooser.showDialog(new JLabel(), XML_BTN_STR);
                xmlFile = fileChooser.getSelectedFile();

                if (xmlFile == null) {
                    xmlBtn.setEnabled(true);
                } else {
                    // 校验失败, 直接返回
                    if (!checkFile(xmlFile, "xml")) {
                        xmlFile = null;
                        return;
                    }
                    xmlBtn.setEnabled(false);
                    if (!excelBtn.isEnabled()) {
                        okBtn.setEnabled(true);
                    }
                }
                break;
            }
            case XLS_BTN_STR: {
                fileChooser = new JFileChooser(PATH);
                fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                fileChooser.showDialog(new JLabel(), XLS_BTN_STR);
                xlsFile = fileChooser.getSelectedFile();

                if (xlsFile == null) {
                    excelBtn.setEnabled(true);
                } else {
                    // 校验失败, 直接返回
                    if (!checkFile(xlsFile, "xls")) {
                        xlsFile = null;
                        return;
                    }
                    excelBtn.setEnabled(false);
                    if (!xmlBtn.isEnabled()) {
                        okBtn.setEnabled(true);
                    }
                }
                break;
            }
            case ENTER_OK: {
                Xls2Xml translateTool = new Xls2Xml();
                System.out.println("xlsFile: " + xlsFile + " xmlFile: " + xmlFile + " dest: "
                        + dest);
                try {
                    translateTool.translate(xlsFile, xmlFile, dest);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
                JOptionPane.showMessageDialog(new JButton("确定"), "生成文件路径为：" + dest);
                okBtn.setEnabled(false);
                break;
            }
            case ENTER_CANCEL: {
                xlsFile = null;
                xmlFile = null;
                excelBtn.setEnabled(true);
                xmlBtn.setEnabled(true);
                okBtn.setEnabled(false);
                break;
            }
        }
    }

    /**
     * 检查文件格式是否正确
     * 
     * @param file: 要检查的文件
     * @param selfSuffix: 匹配的后缀,如 xls(不包含 . )
     * @return 校验合法返回true, 否则返回false
     */
    private boolean checkFile(File file, String selfSuffix) {
        String fileName = file.getName();
        String suffix = fileName.substring(fileName.indexOf(".") + 1, fileName.length());
        if (!suffix.equalsIgnoreCase(selfSuffix)) { // 选择的文件类型不对
            JOptionPane.showMessageDialog(new JButton("确定"), "文件类型错误！");
            return false;
        } else {
            return true;
        }
    }

}
